<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { showToast } from 'vant';
import { UserControllerService } from '../../api/services/UserControllerService'

const router = useRouter();
const username = ref('');  // 假设对应接口的email字段
const password = ref('');  // 对应接口的pwd字段
const confirmPassword = ref('');
// 如果需要性别字段，可添加：const gender = ref('男');

const onSubmit = async () => {
  try {

    const res = await UserControllerService.addUser({
      username: username.value,
      email: username.value,
      password: password.value
    })
    if (res.code === 0) {
      console.log(res);
      showToast('注册成功,请登录');
      router.push({ name: 'Login' });
    }
  } catch (error) {
    showToast('注册失败,请检查网络配置');
  }
};

const goLogin = () => {
  router.push({ name: 'Login' });
};
</script>

<template>
  <div class="register-container">
    <div class="background-overlay"></div>
    <div class="register-content">
      <div class="top">
        <van-image class="logo animated-logo" src="/src/assets/ico.png" round fit="cover" />
      </div>
      <van-form @submit="onSubmit" class="register-form">
        <van-field
          v-model="username"
          name="username"
          label="用户名/邮箱"
          placeholder="请输入邮箱"
          :rules="[{ required: true, message: '请填写邮箱' }]"
          class="field-item"
        />
        <van-field
          v-model="password"
          type="password"
          name="password"
          label="密码"
          placeholder="请输入密码"
          :rules="[{ required: true, message: '请填写密码' }]"
          class="field-item"
        />
        <van-field
          v-model="confirmPassword"
          type="password"
          name="confirmPassword"
          label="确认密码"
          placeholder="请再次输入密码"
          :rules="[{ required: true, message: '请确认密码' }]"
          class="field-item"
        />
      </van-form>
      <van-button round block type="primary" native-type="submit" @click="onSubmit" class="register-button" color="var(--gradient-primary)">
        注册
      </van-button>
      <div class="bottom">
        <div class="login">已有账号？<span class="protocol" @click="goLogin"  color="var(--gradient-primary)">点击登录</span></div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.register-container {
    padding: 20px;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    overflow: hidden;
}

.background-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(255, 255, 255, 0.8); /* 半透明遮罩 */
    z-index: -1;
}

.register-content {
    width: 100%;
    max-width: 400px;
    padding: 40px;
    background: white;
    border-radius: 16px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.top {
    display: flex;
    justify-content: center;
    margin-bottom: 40px;
}

.animated-logo {
    width: 120px;
    height: 120px;
    animation: fadeIn 1s ease-out;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.register-form {
    margin-bottom: 30px;
}

.field-item {
    margin-bottom: 20px;
}

.register-button {
    margin-top: 30px;
    background: linear-gradient(45deg, #007bff, #00c6ff); /* 渐变按钮 */
    border: none;
    transition: transform 0.2s;
}

.register-button:active {
    transform: scale(0.98);
}

.bottom {
    display: flex;
    justify-content: center;
    margin-top: 20px;
}

.login {
    color: #666;
    font-size: 14px;
}

.protocol {
    color: #007bff;
    text-decoration: underline;
    cursor: pointer;
}
</style>
